package Controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import POJO.Category;
import POJO.Customer;
import POJO.CustomerType;
import POJO.Image;
import POJO.Product;
import POJO.ProductDetail;
import POJO.TypeDetail;
import POJO.User;
import Service.AdminService;
import Service.HomeService;


@Controller
@Scope("request")
public class AdminController {
	@Autowired
	private AdminService adminService;

	@Autowired
	private HomeService homeService;

	@Autowired
	private POJO.Cart cart;
	
	@RequestMapping("admin/admin")
	public ModelAndView showInfo()
	{
		System.out.println("======admin======" +cart.getCartDetails().size());
		return new ModelAndView("admin");
	}

	@RequestMapping("admin/category")
	public ModelAndView showCategory(@ModelAttribute("command") Category category) {
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("newmodel", homeService.createTreeMenu());
		model.put("category", adminService.getCategoryById(category.getCategoryId()));
		model.put("categoryParent", adminService.getCategoryById(adminService
				.getCategoryById(category.getCategoryId()).getParentId()));
		model.put("categories", adminService.getListCategory());
		//System.out.println("Id  " + category.getCategoryId());
		return new ModelAndView("category", model);
	}

	@RequestMapping(value="admin/saveCategory", method = RequestMethod.POST)
	public ModelAndView saveCategory(@ModelAttribute("command") Category category, 
			BindingResult result) {
		adminService.saveCategory(category);
		//System.out.println("Id  " + category.getCategoryId());
		return new ModelAndView("redirect:/admin/category.html?categoryId=" + category.getCategoryId());
	}
	
	@RequestMapping(value = "admin/addCategory", method = RequestMethod.POST)
	public ModelAndView addCategory(@ModelAttribute("command") Category category,
			BindingResult result) {

		adminService.addCategory(category);
		
		return new ModelAndView("redirect:/admin/category.html?categoryId=" + category.getCategoryId());
	}
	
	@RequestMapping(value = "admin/deleteCategory", method = RequestMethod.GET)
	public ModelAndView deleteCategory(@ModelAttribute("command") Category category,
			BindingResult result) {
		
		adminService.deleteCategory(category);		

		return new ModelAndView("redirect:/admin/category.html?categoryId=0");
	}
	
	
	@RequestMapping("admin/products")
	public ModelAndView showProducts(@ModelAttribute("command") Product product) {
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("newmodel", homeService.createTreeMenu());
		model.put("products", adminService.getListProducts());
		//System.out.println("Id  " + category.getCategoryId());
		return new ModelAndView("products", model);
	}
	
	@RequestMapping("admin/product")
	public ModelAndView showProduct(@ModelAttribute("command") Product product) {
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("newmodel", homeService.createTreeMenu());
		model.put("product", adminService.getProducById(product.getProductId()));
		model.put("categories", adminService.getListCategory());		
		
		String date = adminService.getProducById(product.getProductId()).getDateCreate().toString();
		date = date.substring(0, 10);
		model.put("date", date);
		//System.out.println("date  " + date);
		return new ModelAndView("product", model);
	}
	
	@RequestMapping(value = "admin/addproduct")
	public ModelAndView addproduct(@ModelAttribute("command") Product product) {		
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("newmodel", homeService.createTreeMenu());
		model.put("categories", adminService.getListCategory());	
		
		System.out.println(product.getProductName());
		return new ModelAndView("addproduct", model);
	}
	
	@RequestMapping(value="admin/createProduct",  method = RequestMethod.POST)
	public ModelAndView createProduct(@ModelAttribute("command") Product product, BindingResult result) {
		adminService.createProduct(product);
		System.out.println("date  " + product.getDateCreate());
		return new ModelAndView("redirect:/admin/products.html");
	}
	
	@RequestMapping(value="admin/saveProduct",  method = RequestMethod.POST)
	public ModelAndView saveProduct(@ModelAttribute("command") Product product, BindingResult result) {
		adminService.saveProduct(product);
		System.out.println("date  " + product.getDateCreate());
		
		return new ModelAndView("redirect:/admin/products.html");
	}
	
	@RequestMapping(value = "admin/deleteProduct", method = RequestMethod.GET)
	public ModelAndView deleteProduct(@ModelAttribute("command") Product product,
			BindingResult result) {
		
		adminService.deleteProduct(product);		

		return new ModelAndView("redirect:/admin/products.html");
	}
	
	@RequestMapping(value = "admin/productDetails", method = RequestMethod.GET)
	public ModelAndView productDetails(@ModelAttribute("command") Product product,
			BindingResult result) {
		Map<String, Object> model = new HashMap<String, Object>();
		
		model.put("newmodel", homeService.createTreeMenu());
		model.put("product", adminService.getProductById(product.getProductId()));
		model.put("types", homeService.getTypes());
		model.put("productDetails", adminService.getListProductDetail(product.getProductId()));

		return new ModelAndView("productDetails", model);
	}
	
	@RequestMapping("admin/productDetail")
	public ModelAndView productDetail(@ModelAttribute("command") ProductDetail productDetail) {
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("newmodel", homeService.createTreeMenu());	
		
		model.put("productDetail", adminService.getProductDetail(productDetail.getProductDetailId()));
		model.put("images", adminService.getListImage());
		model.put("types", homeService.getTypes());
		model.put("typeDetails", homeService.getTypeDetails());
				
		return new ModelAndView("productDetail", model);
	}
	
	@RequestMapping("admin/addProductDetail")
	public ModelAndView addProductDetail(@ModelAttribute("command") ProductDetail productDetail, @RequestParam("productId") int productId ) {
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("newmodel", homeService.createTreeMenu());		
		model.put("product", adminService.getProductById(productId));
		
		model.put("images", adminService.getListImage());
		model.put("types", homeService.getTypes());
		model.put("typeDetails", homeService.getTypeDetails());
				
		return new ModelAndView("addProductDetail", model);
	}
	
	@RequestMapping(value="admin/createProductDetail",  method = RequestMethod.POST)
	public ModelAndView createProductDetail(@ModelAttribute("command") ProductDetail productDetail, BindingResult result) {
				
		
		//System.out.println("date  " + product.getDateCreate());		
		
		
		for (Image i : productDetail.getImages()) {
			i.setImageId(Integer.parseInt(i.getImageUrl()));			
		}
		
		for (Image i : adminService.getListImage()) {
			for (Image ii : productDetail.getImages()) {
				if(i.getImageId() == ii.getImageId())
					ii.setImageUrl(i.getImageUrl());
			}			
		}
		
		
		
		System.out.println(productDetail.getTypeDetails().size());
		for (TypeDetail type : productDetail.getTypeDetails()) {
			System.out.println(type.getTypeDetailName());
		}
		
		/*for (Image i : productDetail.getImages()) {
			System.out.println(i.getImageUrl());			
		}*/
		
		adminService.createProductDetail(productDetail);
		
		
		return new ModelAndView("redirect:/admin/products.html");
	}
	
	@RequestMapping(value="admin/saveProductDetail",  method = RequestMethod.POST)
	public ModelAndView saveProductDetail(@ModelAttribute("command") ProductDetail productDetail, BindingResult result) {
				
		
		//System.out.println("date  " + product.getDateCreate());		
		
		
		for (Image i : productDetail.getImages()) {
			i.setImageId(Integer.parseInt(i.getImageUrl()));			
		}
		
		for (Image i : adminService.getListImage()) {
			for (Image ii : productDetail.getImages()) {
				if(i.getImageId() == ii.getImageId())
					ii.setImageUrl(i.getImageUrl());
			}			
		}		
		
		
		System.out.println(productDetail.getTypeDetails().size());
		for (TypeDetail type : productDetail.getTypeDetails()) {
			System.out.println(type.getTypeDetailName());
		}
		
		/*for (Image i : productDetail.getImages()) {
			System.out.println(i.getImageUrl());			
		}*/
		
		adminService.saveProductDetail(productDetail);
		
		
		return new ModelAndView("redirect:/admin/products.html");
	}
	
	
	
	@RequestMapping("admin/accounts")
	public ModelAndView showAccounts(@ModelAttribute("command") Customer cus) {
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("newmodel", homeService.createTreeMenu());
		model.put("accounts", adminService.getListAccount());
		//System.out.println("Id  " + category.getCategoryId());
		return new ModelAndView("accounts", model);
	}
	
	@RequestMapping("admin/account")
	public ModelAndView showAccount(@ModelAttribute("command") Customer cus) {
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("newmodel", homeService.createTreeMenu());
		model.put("account", adminService.getAccountById(cus.getCustomerId()));
		//System.out.println("Id  " + category.getCategoryId());
		return new ModelAndView("account", model);
	}
	
	@RequestMapping(value="admin/saveAccount",  method = RequestMethod.POST)
	public ModelAndView saveAccount(@ModelAttribute("command") Customer cus, BindingResult result) {
		adminService.saveAccount(cus);
				
		return new ModelAndView("redirect:/admin/accounts.html");
	}
}
